package com.example.administrator.fangbook.read;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.administrator.fangbook.HtlMvp.activity.BaseMvpActivity;
import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.dataLibrary.book.IBookbase;
import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.read.bean.ChineseallBookReadStartData;
import com.example.administrator.fangbook.read.bean.SOURCE_TYPE;
import com.example.administrator.fangbook.read.presenter.ReadPresenter;
import com.example.administrator.fangbook.read.view.IReadView;
import com.example.administrator.fangbook.read.view.readView.ZLAndroidWidget;
import com.example.administrator.fangbook.utils.ToastUtils;

import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.formats.BookReader;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.ui.android.view.animation.AnimationManager;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ReadActivity extends BaseMvpActivity<IReadView, ReadPresenter> implements IReadView {
    public static final String mBookStartReadDataKey = "StartReadDataKey";
    @Bind(R.id.main_view)
    ZLAndroidWidget mReadView;
    /**
     * 和动画交互的类
     */
    private AnimationManager mAnimationManager;
    /**
     * 注册功能类
     */
    private FBReaderApp mReadApp;

    @Override
    protected boolean getIsHideState() {
        return true;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mReadApp = new FBReaderApp(this);
        mAnimationManager = new AnimationManager(mReadView);
        ZLFile zlFile = getmMvpPresenter().fromIntent();
        if (zlFile == null) {
            ToastUtils.show("打开阅读器失败");
            finish();
        }
        //创建BookRead 把file数据转化成BookReader对象
        BookReader reader = BookReader.createReader(mReadApp, zlFile);

        reader.setAnimationManager(mAnimationManager);

        //初始化线程、获取默认设置的动画
        mAnimationManager.start();
        //BookRead--转换成BookModel
        reader.start();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_read;
    }

    @Override
    public ReadPresenter bindMvpPresenter() {
        return new ReadPresenter();
    }

    @Override
    public IReadView bindMvpView() {
        return this;
    }

    /**
     * 启动阅读页
     *
     * @param activity
     * @param book
     */
    public static void startThis(Activity activity, ShelfItemBook book) {
        Intent intent = new Intent(activity, ReadActivity.class);
        ChineseallBookReadStartData startData = new ChineseallBookReadStartData();
        if (book.getBookType() == IBookbase.BookType.Type_ChineseAll || book.getBookType() == IBookbase.BookType.Type_Migu) {
            //中文书城的书和咪咕的书
            startData.setChapterId("");
            startData.setSourceType(SOURCE_TYPE.ST_BOOKINTRODUCTION);
        } else if (book.getBookType() == IBookbase.BookType.Type_Epub) {
            //epub--本地epub的书
            startData.setSourceType(SOURCE_TYPE.ST_LOCALFILE_EPUB);
        } else if (book.getBookType() == IBookbase.BookType.Type_Txt) {
            //txt--本地txt的书
            startData.setChapterId("");
            startData.setSourceType(SOURCE_TYPE.ST_LOCALFILE_TXT);
        }
        startData.setShelfBook(book);
        intent.putExtra(mBookStartReadDataKey, startData);
        activity.startActivity(intent);
    }

}
